<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Field_Abstract {

  protected $_errors = array();
  protected $_type;
  protected $_name;
  protected $_value;
  protected $_params;

  public function __construct($name, $params = NULL) {
    $this->_name = $name;
    $this->_params = (array) $params;
  }

  public function getHtml() {
    throw new Exception("class " . __CLASS__ . " is not implemented!");
  }

  public function getLabel() {
    return @$this->_params['label'];
  }

  public function setLabel($label) {
    $this->_params['label'] = $label;
  }

  public function getName() {
    return @$this->_name;
  }

  public function setName($name) {
    $this->_name = $name;
  }

  public function getValue() {
    return $this->_value;
  }

  public function setValue($value) {
    $this->_value = $value;
  }

  public function getExtras() {
    if (is_array(@$this->_params['extras'])) {
      $str = '';
      foreach ($this->_params['extras'] as $key => $value) {
        $str .= sprintf('%s="%s"', $key, $value) . " ";
      }
      return $str;
    }
    return '';
  }

  public function setExtras($name, $value) {
    $this->_params['extras'][$name] = $value;
  }

  public function isValid() {
    if (($rules = $this->getRules()) != NULL) {
      list($result, $errors) = Validate::isValid($rules, $this->getValue());
      $this->_errors = $errors;
      return $result;
    }
  }

  public function getRules() {
    return @$this->_params['rules'];
  }

  public function getErrors() {
    return $this->_errors;
  }

  public function setErrors($errors) {
    $this->_errors = (array) $errors;
  }

}